package com.mifengs.mall.ui.main;

import android.text.TextUtils;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.mifengs.commonbase.util.DeviceInfoUtil;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.mall.Constant;
import com.mifengs.mall.MallApplication;
import com.mifengs.mall.entity.ResponseBean;
import com.mifengs.mall.entity.ResponseInfo;
import com.mifengs.mall.entity.SplashBean;
import com.mifengs.mall.entity.SplashResponseBean;
import com.mifengs.mall.entity.StoreApplyInfo;
import com.mifengs.mall.entity.UpdateData;
import com.mifengs.mall.entity.UpgradeInfo;
import com.mifengs.mall.netWork.NetWork;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import rx.Observer;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by lihaoqi on 2017/10/30.
 */

public class MainPresenter implements MainContract.Presenter{


    private final MainContract.View mView;

    public MainPresenter(MainContract.View mMainView) {
        this.mView = mMainView;
    }


    @Override
    public void getUpdateInfo() {
        HashMap<String,String> hashMap = new HashMap<>();
        hashMap.put("platform","1");
        NetWork.getApplyGoodsApi().getInfo(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseInfo<UpdateData>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseInfo<UpdateData> updateDataResponseInfo) {
                        if(updateDataResponseInfo.getStatus()== Constant.OK_CODE){
                            try {
                                UpdateData bean = updateDataResponseInfo.getResult();
                                String webVerCode = bean.getVersion();
                                String nowVerCode = DeviceInfoUtil.getAppVersion(MallApplication.getInstance());
                                if(webVerCode.compareTo(nowVerCode)>0){
                                    mView.showUpdateDialog(bean);
                                }
                            }catch (Exception e){

                            }
                        }
                    }
                });
    }

    @Override
    public void appLaunch() {
        HashMap<String,Object> hashMap = new HashMap<>();
        if(!TextUtils.isEmpty(PreferenceUtils.getToken())){
            hashMap.put("app_token",PreferenceUtils.getToken());
        }
        NetWork.getAppLaunchApi().appLaunch(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
//                        mView.showLongToast(e.toString());
                    }

                    @Override
                    public void onNext(ResponseInfo responseInfo) {
//                        mView.showLongToast(responseInfo.getStatus()+" "+responseInfo.getMsg());
                    }
                });
    }

    @Override
    public void getPhotoPath() {
        NetWork.getHomeDataApi().getPhotoPath()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseInfo<String>>bindToLife())
                .subscribe(new Subscriber<ResponseInfo<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseInfo<String> stringResponseInfo) {
                        if(stringResponseInfo.getStatus()==Constant.OK_CODE){
                            PreferenceUtils.savePhotoPath(mView.getContext(),stringResponseInfo.getResult());
                        }
                    }
                });
    }


    @Override
    public void getApplyProgress() {
        mView.showLoading();
        HashMap<String,String> hashMap = new HashMap<>();
        if(!TextUtils.isEmpty(PreferenceUtils.getToken())){
            hashMap.put("app_token",PreferenceUtils.getToken());
        }else{
            hashMap.put("app_token","");
        };
        NetWork.getHomeDataApi().getApplyProgress(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean<StoreApplyInfo>>bindToLife())
                .subscribe(new Observer<ResponseBean<StoreApplyInfo>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.hideLoading();
//                        mView.showLongToast(e.toString());
                    }

                    @Override
                    public void onNext(ResponseBean<StoreApplyInfo> storeApplyInfoResponseBean) {
                        mView.hideLoading();
                        String status = storeApplyInfoResponseBean.getStatus();
                        if(Constant.OK_STATUS.equals(status)){
                            StoreApplyInfo info = storeApplyInfoResponseBean.getParamer();
                            int storeStatus = info.getStoreApplyInfoDto().getStoreStatus();
                            int isExist = info.getStoreApplyInfoDto().getIsExist(); // 0 为不存在 1 为存在
                            int type = info.getStoreApplyInfoDto().getType(); //0 个体 1 企业
             ;               if(isExist==0){
                                mView.startShopTypeActivity();
                            }else if (isExist==1){
//                                switch (storeStatus){
//                                    case -1:
//
//                                        break;
//                                    case 1:
//
//                                        break;
//                                    case 5:
//
//                                        break;
//                                }
                                mView.startOpenShopActivity(type,storeStatus,info);
                            }
                        }else if ("-1".equals(status)){
                            mView.startLoginActivity();
                        }
                    }
                });
    }

    @Override
    public void getSplashInfo() {
        NetWork.getHomeDataApi().getSplashScreen()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean<SplashResponseBean>>bindToLife())
                .subscribe(new Observer<ResponseBean<SplashResponseBean>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable throwable) {

                    }

                    @Override
                    public void onNext(ResponseBean<SplashResponseBean> splashResponseBeanResponseBean) {
                        if (splashResponseBeanResponseBean.getStatus().equals(Constant.SUCCESS_CODE)){
                            ArrayList<SplashBean> splashBeans =  splashResponseBeanResponseBean.getParamer().getSplashScreens();
                            if(splashBeans.size()>0){
                                SplashBean bean = splashBeans.get(0);
                                preloadSplashImg(bean);
                            }
                        }
                    }
                });
    }

    @Override
    public void getUpgradeInfo() {
        NetWork.getHomeDataApi().getUpgradeInfo()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(mView.<ResponseBean<UpgradeInfo>>bindToLife())
                .subscribe(new Observer<ResponseBean<UpgradeInfo>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(ResponseBean<UpgradeInfo> responseBean) {
                        String status = responseBean.getStatus();
                        if (status.equals(Constant.SUCCESS_CODE)) {
                            UpgradeInfo upgradeInfo= responseBean.getParamer();
                            if(upgradeInfo.getNeedUpgrade() == 1){
                                mView.upgradeInfo(upgradeInfo);
                            }
                        }
                    }
                });
    }


    public void preloadSplashImg(final SplashBean bean){
        final String path = bean.getResourcesPath();
        Glide.with(mView.getContext())
                .load(path)
                .downloadOnly(
                        new SimpleTarget<File>() {
                            @Override
                            public void onResourceReady(File resource, GlideAnimation<? super File> glideAnimation) {
                                PreferenceUtils.saveSplashImg(mView.getContext(),path);
                                String status = bean.getResourcesStatus();
                                String time = bean.getShowTime();
                                String type = bean.getResourcesType();
                                String route = bean.getResourcesRoute();
                                PreferenceUtils.saveStringPreference(mView.getContext(),PreferenceUtils.LocalUser.SPLASH_ROUTE,route);
                                PreferenceUtils.saveStringPreference(mView.getContext(),PreferenceUtils.LocalUser.SPLASH_STATUS,status);
                                PreferenceUtils.saveStringPreference(mView.getContext(),PreferenceUtils.LocalUser.SPLASH_TIME,time);
                                PreferenceUtils.saveStringPreference(mView.getContext(),PreferenceUtils.LocalUser.SPLASH_TYPE,type);

                            }
                        }
                );
    }



}
